<?php

namespace app\index\model;

use think\Db;

/**
 * @author xjy
 */
class ProduceModel
{
    private static $salesState=["1"=>"新产品","2"=>"清仓品","3"=>"在售品","4"=>"下架品"];  //销售状态
    private static $cycleStage=["1"=>"补货期","2"=>"新品期","3"=>"计划淘汰期","4"=>"淘汰期"];  //生命周期

    //用户列表
    static public function getSkuLifeCycleList($param, $titles = null, $function = null)
    {
        $where=[];
        $db = Db::table('sku_life_cycle')
                ->alias('a');
        if (!empty($param['sku'])) {
            $where['sku']=$param['sku'];
        }
        if (!empty($param['sales_state'])) {
            $where['sales_state']=$param['sales_state'];
        }
        if (!empty($param['cycle_stage'])) {
            $where['cycle_stage']=$param['cycle_stage'];
        }
        if (!empty($param['sku_grade'])) {
            $where['sku_grade']=$param['sku_grade'];
        }
        if (!empty($param['is_time'])) {
            $where['is_time']=$param['is_time'];
        }
        $fields='id,sku,cname,display_classified,classified_one,classified_two,classified_three,classified_four,sales_state,'
                . 'cycle_stage,sku_grade,is_time,seasonal_cycle,status,operator,update_time';
        $db->field($fields);
        $db->where($where);
        $db->order('id','desc');
        return \app\common\service\ToolService::downOrReturn($db, $param, $titles,$function);
    }
    //用户列表
    static public function getCountrySkuList($param, $titles = null, $function = null)
    {
        $where=[];
        $db = Db::table('country_sku_status')
                ->alias('a');
        if (!empty($param['sku'])) {
            $where['sku']=$param['sku'];
        }
        if (!empty($param['country_name1'])) {
            $where['country_name']=['like',$param['country_name1']."%"];
        }
        if (!empty($param['sales_state'])) {
            $where['sales_state']=$param['sales_state'];
        }
        if (!empty($param['sales_distributed'])) {
            $where['sales_distributed']=$param['sales_distributed'];
        }
        if (!empty($param['sku_grade'])) {
            $where['sku_grade']=$param['sku_grade'];
        }
        if (!empty($param['is_time'])) {
            $where['is_time']=$param['is_time'];
        }
        $fields='id,sku,cname,country_name,sales_distributed,sales_state,'
                . 'sku_grade,is_time,seasonal_cycle,status,operator,update_time';
        $db->field($fields);
        $db->where($where);
        $db->order('id','desc');
        return \app\common\service\ToolService::downOrReturn($db, $param, $titles,$function);
    }
    //店铺sku状态列表
    static public function getStoreSkuList($param, $titles = null, $function = null)
    {
        $where=[];
        $db = Db::table('store_sku')
                ->alias('a');
        if (!empty($param['sku'])) {
            $where['sku']=$param['sku'];
        }
        if (!empty($param['shop1'])) {
            $where['shop']=['like',$param['shop1']."%"];
        }
        if (!empty($param['sales_state'])) {
            $where['sales_state']=$param['sales_state'];
        }
        if (!empty($param['sku_grade'])) {
            $where['sku_grade']=$param['sku_grade'];
        }
        $fields='id,sku,cname,shop,sales_state,cycle_stage,'
                . 'sku_grade,status,operator,update_time';
        $db->field($fields);
        $db->where($where);
        $db->order('id','desc');
        return \app\common\service\ToolService::downOrReturn($db, $param, $titles,$function);
    }
    //获取sku生命周期信息
    static public function getSkuCycle($where=[])
    {
        $fields='SKU,cname,display_classified,classified_one,classified_two,classified_three,classified_four,sales_state,'
                . 'cycle_stage,sku_grade,is_time,seasonal_cycle,status';
        $list=[];
        $skulife=\app\common\model\ToolModel::select('sku_life_cycle',$where,$fields,'','SKU');
        if($skulife){
            foreach($skulife as $key=>&$value){
                $value['cycle_stage_name']=isset(self::$cycleStage[$value['cycle_stage']]) ? self::$cycleStage[$value['cycle_stage']]:'';
                $value['sales_state_name']=isset(self::$salesState[$value['sales_state']]) ? self::$salesState[$value['sales_state']]:'';
                $value['is_time']=$value['is_time']==1 ? '是' :'否';
                $value['status_name']=$value['status']==1 ? '正常' :'禁止';
            }
        }
        return $skulife;
    }
}
